﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aulac.Rsc.Bss.Tags;
using Aulac.Client.Global.Interface;
using System.Windows.Forms;
using Aulac.Rsc.Bss;
using Aulac.Global.Common;
using Aulac.UtilityTools;

namespace Aulac.Client.Bss.WinForm.CapAss
{
    public partial class FormDlgCapAss
    {
        #region Override
        public override void OnFormLoad()
        {
            base.OnFormLoad();
            cboCapAssGrp.AutoLoadData = true;
            this.AulacNavCtrl.ShowControlAll(false);
            this.AulacNavCtrl.ShowControl(true, AulacNavCtrl.DelControl, AulacNavCtrl.SaveControl, AulacNavCtrl.ExitControl);
            panelTTKH.Expand = false;
            this.Height = 450;            
            txtCapAssID.Focus();
            
        }

        protected override void OnSetStateControls(Aulac.Global.Enums.FormModes pFormMode)
        {
            base.OnSetStateControls(pFormMode);
            this.AulacNavCtrl.ShowControlAll(false);
            if (pFormMode == Aulac.Global.Enums.FormModes.LockMode || pFormMode == Aulac.Global.Enums.FormModes.ViewMode)
            {
                foreach (IAulacStateCtrl ctr in GetAllControlsForm())
                    ctr.ReadOnly = true;
                this.AulacNavCtrl.SetStateControl(pFormMode);
            }
            else
            {
                foreach (IAulacStateCtrl ctr in GetAllControlsForm())
                    ctr.ReadOnly = false;
            }
            if (pFormMode == Aulac.Global.Enums.FormModes.NewMode)
                chkIsActive.Checked = true;                
            foreach (IAulacStateCtrl ctr in GetListAulacDataControls(panelTTKH))
                ctr.ReadOnly = true;
            btnNav.ShowButton(true, btnNav.btnSave, btnNav.btnExit, btnNav.btnDel);
            btnNav.WidthButton = 100;
            this.AulacNavCtrl.SetStateControl(pFormMode);
        }
        protected override bool OnShowDetailVoucher()
        {
            //if(cboMdnItem.IsEmptyDataInList)
            cboDept.AutoLoadData = true;
            cboDept.ReLoadDataToControl();
            cboProvider.AutoLoadData = true;
            cboProvider.ReLoadDataToControl();
            cboStaff.AutoLoadData = true;
            cboStaff.ReLoadDataToControl();
            cboCapAssGrp.AutoLoadData = true;
            cboCapAssGrp.ReLoadDataToControl();
            return GetDetailItemCapAss();
        }
        protected override void OnClearValueControls()
        {
            ClearValueToControls();
        }
        protected override void OnAddNewVoucher()
        {
            ClearValueToControls();
            base.OnAddNewVoucher();
            txtCapAssID.Focus();
        }
        protected override void OnEditVoucher()
        {
            cboCapAssGrp.AutoLoadData = true;
            cboCapAssGrp.ReLoadDataToControl();
            base.OnEditVoucher();
            GetDetailItemCapAss();            
            txtCapAssID.Focus();
        }
        protected override bool OnBeginSaveVoucher()
        {
            if (txtCapAssID.IsEmpty)
            {
                MsgBox.Show("Mã tài sản không được rỗng !", MsgBox.Title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtCapAssID.Focus();
                return false;
            }
            if (txtCapAssName.IsEmpty)
            {
                Aulac.UtilityTools.MsgBox.Show("Tên tài sản không được rỗng !", "AulacSoft", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtCapAssName.Focus();
                return false;
            }
            return base.OnBeginSaveVoucher();
        }        
        protected override bool OnSaveVoucherData(ref System.Xml.XmlDataDocument xmlData)
        {
            return SaveItemCapAss(ref xmlData);
        }
        protected override void OnEndSaveVoucher(Aulac.Global.Enums.FormModes pEndFormMode, System.Xml.XmlDataDocument xmlData)
        {
            base.OnEndSaveVoucher(pEndFormMode, xmlData);
        }
        protected override bool OnDeleteDataVoucher()
        {
            return DeleteCapAss();
        }
        protected override bool OnBeginExit()
        {
            if (FormMode == Aulac.Global.Enums.FormModes.EditMode || FormMode == Aulac.Global.Enums.FormModes.NewMode)
                return true;
            else
                FormMode = Aulac.Global.Enums.FormModes.ViewMode;
            return true;
        }
        #endregion

        #region Method
        private void Init()
        {
            cboOrgAcc.LstAccTypes = new List<AccTypes> { AccTypes.VCSH };
            cboDeprAcc.LstAccTypes = new List<AccTypes> { AccTypes.TSNH, AccTypes.TSDH};
            cboCostAcc.LstAccTypes = new List<AccTypes> { AccTypes.DT };
        }
        private void SetFieldMember()
        {
            // ---------------- SET FieldMember ------------------
            txtAttHrdPlan.FieldMember = tagItems.CapAss.AttHrdPlan;            
            txtCapAssID.FieldMember = tagItems.CapAss.CapAssID;
            txtCapAssName.FieldMember = tagItems.CapAss.CapAssName;
            txtDeprMRate.FieldMember = tagItems.CapAss.DeprMonthRate;
            txtDeprMValue.FieldMember = tagItems.CapAss.DeprMonthValue;
            txtDeprValue.FieldMember = tagItems.CapAss.DeprValue;
            txtDeprYRate.FieldMember = tagItems.CapAss.DeprYearRate;
            txtDeprYValue.FieldMember = tagItems.CapAss.DeprYearValue;            
            txtDscrb.FieldMember = tagItems.CapAss.Describe;
            txtOrgPrice.FieldMember = tagItems.CapAss.OriginalPrice;
            txtPrdYear.FieldMember = tagItems.CapAss.ProductYear;
            txtRestOfValue.FieldMember = tagItems.CapAss.RestOfValue;
            txtSerialNum.FieldMember = tagItems.CapAss.Serial;
            txtSrvcLife.FieldMember = tagItems.CapAss.SrvcLife;
            txtWrrty.FieldMember = tagItems.CapAss.Warranty;
            
            cboCapAssGrp.AddFieldMapping(tagItems.CapAssGrp.CapAssGrpPrkID, tagItems.CapAss.CapAssGrpPrkID);
            cboCostAcc.AddFieldMapping(tagItems.SysAcc.AccPrkID, tagItems.CapAss.CostAccPrkID);
            cboCountry.AddFieldMapping(tagItems.Country.CountryPrkID, tagItems.CapAss.CountryPrkID);
            cboDeprAcc.AddFieldMapping(tagItems.SysAcc.AccPrkID, tagItems.CapAss.DeprAccPrkID);
            cboDeprMethod.AddFieldMapping(tagItems.SysAcc.AccPrkID, tagItems.CapAss.DeprMthdPrkID);
            cboDept.AddFieldMapping(tagItems.Department.DeptPrkID, tagItems.CapAss.DeptPrkID);
            cboOrgAcc.AddFieldMapping(tagItems.SysAcc.AccPrkID, tagItems.CapAss.OrgPrAccPrkID);
            cboProvider.AddFieldMapping(tagItems.Provider.ProviderPrkID, tagItems.CapAss.ProviderPrkID);
            cboStaff.AddFieldMapping(tagItems.Staff.StaffPrkID, tagItems.CapAss.StaffPrkID);
            cboState.AddFieldMapping(tagItems.State.StatePrkID, tagItems.CapAss.StatePrkID);
            cboTimeUseUnit.AddFieldMapping(tagItems.TimeUseUnit.UnitPrkID, tagItems.CapAss.TimeUseUnitPrkID);
            //cboDept.FieldMember = tagItems.CapAss.DeptPrkID;
            //cboProvider.FieldMember = tagItems.CapAss.ProviderPrkID;
            //cboStaff.FieldMember = tagItems.CapAss.StaffPrkID;
            chkIsActive.FieldMember = tagItems.CapAss.IsActive;
        }

        /// <summary>
        /// Xóa dữ liệu các Controls trong Form.
        /// </summary>
        public void ClearValueToControls()
        {
            foreach (IAulacValueDataCtrl ctr in GetAllControlsForm())
                ctr.ClearValueControls();
        }

        private List<IAulacDataControl> GetAllControlsForm()
        {
            List<IAulacDataControl> mListControl = new List<IAulacDataControl>();
            GetListAulacDataControls(ref mListControl, grBoxInfo);
            GetListAulacDataControls(ref mListControl, panelTTKH);
            return mListControl;
        }
        #endregion        

        #region Properties
        private string mCapAssID = "";
        public string CapAssID
        {
            get
            { return mCapAssID; }
            set
            { mCapAssID = value; }
        }
        private string mCapAssPrkID = "";
        public string CapAssPrkID
        {
            get
            { return mCapAssPrkID; }
            set
            { mCapAssPrkID = value; }
        }
        private string mTitleName = "";
        public string TitleName 
        {
            get
            { return mTitleName;}
            set
            { 
                mTitleName = value;                
                if (!Aulac.Global.Common.StringHelper.IsEmpty(mTitleName))
                {
                    this.lblTitle.Text = mTitleName;
                    this.Text = Aulac.Global.Common.StringHelper.ReplaceIndex(mTitleName.ToLowerInvariant(), mTitleName.Substring(0, 1).ToLower(), mTitleName.Substring(0, 1).ToUpper(), 0);
                }
            } 
        }
        #endregion
    }
}
